<template>
	<view class="container">
		<Header>学习交流</Header>
		<view class="content">
			<view class="title">
				{{data.title}}
			</view>
			<view class="date">
				<span>{{data.createtime}}</span>
			</view>
			<view v-html="data.content"></view>

		</view>
		<view class="bottom">
			<view class="item" :class="{active: data.like==1}" @click="dlikecCollect(1)">
				<uni-icons type="hand-up-filled" size="30"></uni-icons>
			</view>
			<view class="item" :class="{active: data.collect==1}" @click="dlikecCollect(2)">
				<uni-icons type="star-filled" size="30"></uni-icons>
			</view>
		</view>
		<hb-comment ref="hbComment" @add="add" @del="del" :deleteTip="'确认删除？'" :cmData="commentData"
			v-if="commentData"></hb-comment>
		<!-- <hb-comment ref="hbComment" @add="add" @del="del" @like="like" @focusOn="focusOn" :deleteTip="'确认删除？'"
		    :cmData="commentData" v-if="commentData"></hb-comment> -->
	</view>
</template>

<script>
	import Header from '../../components/header.vue'
	import request from '../../common/request.js'
	export default {
		components: {
			Header
		},
		data() {
			return {
				id: '',
				data: {},
				commentData: {
					readNumer: 0,
					commentSize: 0,
					comment: []
				},
				reqFlag: false,
				comment: {}
			}
		},
		onLoad(option) {
			this.id = option.id
		},
		mounted() {
			this.getDetailData()
		},
		methods: {
			dlikecCollect(type) {
				request({
					url: '/member/dlikecCollect',
					data: {
						fcstudy_id: this.id,
						type: type
					},
					method: 'POST'
				}).then(res => {
					if (res.code == 1) {
						let msg = ''
						if (type == 1) {
							if (this.data.like == 1) {
								this.data.like = 0
								msg = '取消点赞'
							} else {
								this.data.like = 1
								msg = '点赞成功'
							}
						} else {
							if (this.data.collect == 1) {
								this.data.collect = 0
								msg = '取消收藏'
							} else {
								this.data.collect = 1
								msg = '收藏成功'
							}
						}
						uni.showToast({
							title: msg,
							icon: 'none',
							duration: 2000
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			del(id) {
				if (this.reqFlag) {
					uni.showToast({
						title: '操作频繁',
						duration: 1000
					});
					return
				}
				this.reqFlag = true;
				request({
					url: '/member/delComment',
					data: {
						comment_id: id,
					},
					method: 'POST'
				}).then(res => {
					if (res.code == 1) {
						this.reqFlag = false;
						this.$refs.hbComment.deleteComplete(id);
						// this.getCommentList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			add(req) {
				this.comment = req
				if (this.reqFlag) {
					uni.showToast({
						title: '操作频繁',
						duration: 1000
					});
					return
				}
				this.reqFlag = true;
				if (req.pId) {
					this.replayComment()
				} else {
					this.addComment()
				}
			},
			replayComment() {
				request({
					url: '/member/replycomment',
					data: {
						now_id: this.comment.pId,
						content: this.comment.content
					},
					method: 'POST'
				}).then(res => {
					if (res.code == 1) {
						this.reqFlag = false;
						this.$refs.hbComment.addComplete();
						this.getCommentList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			addComment() {
				request({
					url: '/member/onecomment',
					data: {
						fcstudy_id: this.id,
						content: this.comment.content
					},
					method: 'POST'
				}).then(res => {
					if (res.code == 1) {
						this.reqFlag = false;
						this.$refs.hbComment.addComplete();
						this.getCommentList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			getDetailData() {
				request({
					url: '/member/detail',
					data: {
						fcstudy_id: this.id
					},
					method: 'POST'
				}).then(res => {
					if (res.code == 1) {
						this.data = res.data
						if (this.data.type == 2) this.getCommentList()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			getCommentList() {
				request({
					url: '/member/commentList',
					data: {
						fcstudy_id: this.id
					},
					method: 'POST'
				}).then(res => {
					if (res.code == 1) {
						this.commentData.commentSize = res.data.length
						this.commentData.comment = res.data
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 20px;
		line-height: 30px;
	}

	.title {
		font-size: 16px;
		font-weight: bold;
		text-align: center;
		line-height: 26px;
	}

	.date {
		font-size: 14px;
		text-align: center;
		margin: 15px 0px;
	}

	.date span {
		margin: 0px 10px;
	}

	.bottom {
		/* position: sticky;
		bottom: 10px; */
		margin: 10px 0px;
		display: flex;
		align-items: center;
		justify-content: center;

		.item {
			border: 1px solid #e2e2e2;
			display: inline-block;
			width: 50px;
			height: 50px;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 0px 15px;

			&.active {
				::v-deep .uni-icons {
					color: #ea5b6a !important;
				}
			}
		}
	}
</style>